﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using Microsoft.Office.Interop.Excel;

namespace QUANLYDIEMHOCSINH
{
    public partial class XEM_IN_PHIEUDIEM : Form
    {
        HOCKYBUS hkbus = new HOCKYBUS();
        LOPBUS lopbus = new LOPBUS();
        DIEMBUS diembus = new DIEMBUS();
        HOCSINHBUS hsbus = new HOCSINHBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        public XEM_IN_PHIEUDIEM()
        {
            InitializeComponent();
            LoadDanhSachNamHoc();
            LoadDanhSachHocKy();
            LoadDanhSachLop();
            load_ds();
        }

        void LoadDiemHocSinh(int mahk, int mahs, int nam)
        {
            dtgrwDiemHocSinh.DataSource = diembus.LoadDiemHocSinh(mahk,mahs,nam);
        }

        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }

        void LoadDanhSachLop()
        {
            cbLop.DataSource = lopbus.layDSL();
            cbLop.DisplayMember = "TENLOP";
            cbLop.ValueMember = "MALOP";
            cbLop.SelectedItem = 0;
        }

        void LoadDanhSachNamHoc()
        {
            cbNam.DataSource = nambus.LoadDanhSachNamHoc();
            cbNam.DisplayMember = "NAMBATDAU";
            cbNam.ValueMember = "MANAMHOC";
        }
        void load_ds()
        {
            HOCSINHBUS hocsinhbus = new HOCSINHBUS();
            GridTTHS.DataSource = hocsinhbus.dslop(int.Parse(cbLop.SelectedValue.ToString()));
            for (int i = 0; i < GridTTHS.Rows.Count; i++)
            {
                GridTTHS.Rows[i].Cells["stt"].Value = (i + 1).ToString();
            }
            GridTTHS.Columns["MAHS"].Visible = false;
            GridTTHS.Columns[3].Visible = false;
            GridTTHS.Columns[4].Visible = false;
            GridTTHS.Columns[5].Visible = false;
            GridTTHS.Columns[6].Visible = false;
            GridTTHS.Columns[7].Visible = false;
            GridTTHS.Columns[8].Visible = false;
            GridTTHS.Refresh();
        }
        int mahs=0;
        private void GridTTHS_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                lblTen.Text = GridTTHS.Rows[e.RowIndex].Cells["TENHS"].Value.ToString();
                mahs = int.Parse(GridTTHS.Rows[e.RowIndex].Cells["MAHS"].Value.ToString());
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count ; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            GridTTHS.DataSource = hsbus.TimhocSinh(txtTimKiem.Text);
            for (int i = 0; i < GridTTHS.Rows.Count; i++)
            {
                GridTTHS.Rows[i].Cells["stt"].Value = (i + 1).ToString();
            }
            GridTTHS.Refresh();
        }

        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ds();
        }

        private void cbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mahs != 0)
            {
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count - 1; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        private void cbNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mahs != 0)
            {
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count - 1; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            Microsoft.Office.Interop.Excel.Application la = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = la.Workbooks.Add(XlSheetType.xlWorksheet);
            Worksheet ws = (Worksheet)la.ActiveSheet;

            la.Visible = true;
            ws.Cells[1, 1] = "Học sinh:"+lblTen.Text;
           

            ws.Cells[3, 1] = "Môn";
            ws.Cells[3, 2] = "Học kì";
            ws.Cells[3, 3] = "Năm học";
      
            ws.Cells[3, 4] = "Miệng 1";
            ws.Cells[3, 5] = "Miệng 2";
            ws.Cells[3, 6] = "Miệng 3";
            ws.Cells[3, 7] = "15 phút 1";
            ws.Cells[3, 8] = "15 phút 2";
            ws.Cells[3, 9] = "15 phút 3";
            ws.Cells[3, 10] = "15 phút 4";
            ws.Cells[3,11] = "15 phút 5";
            ws.Cells[3,12] = "1 tiết 1";
            ws.Cells[3, 13] = "1 tiết 2";
            ws.Cells[3, 14] = "1 tiết 3";
            ws.Cells[3, 15] = "1 tiết 3";
            ws.Cells[3, 16] = "1 tiết 4";
            ws.Cells[3, 17] = "1 tiết 5";
            ws.Cells[3, 18] = "Thi";
            int i = 4;
            for (int j = 0; j < dtgrwDiemHocSinh.RowCount; j++)
            {
                ws.Cells[i, 1] = dtgrwDiemHocSinh.Rows[j].Cells[0].Value;
                ws.Cells[i, 2] = dtgrwDiemHocSinh.Rows[j].Cells[1].Value;
                ws.Cells[i, 3] = dtgrwDiemHocSinh.Rows[j].Cells[2].Value;
                ws.Cells[i, 4] = dtgrwDiemHocSinh.Rows[j].Cells[3].Value;
                ws.Cells[i, 5] = dtgrwDiemHocSinh.Rows[j].Cells[4].Value;
                ws.Cells[i, 6] = dtgrwDiemHocSinh.Rows[j].Cells[5].Value;
                ws.Cells[i, 7] = dtgrwDiemHocSinh.Rows[j].Cells[6].Value;
                ws.Cells[i, 8] = dtgrwDiemHocSinh.Rows[j].Cells[7].Value;
                ws.Cells[i, 9] = dtgrwDiemHocSinh.Rows[j].Cells[8].Value;
                ws.Cells[i, 10] = dtgrwDiemHocSinh.Rows[j].Cells[9].Value;
                ws.Cells[i, 11] = dtgrwDiemHocSinh.Rows[j].Cells[10].Value;
                ws.Cells[i, 12] = dtgrwDiemHocSinh.Rows[j].Cells[11].Value;
                ws.Cells[i, 13] = dtgrwDiemHocSinh.Rows[j].Cells[12].Value;
                ws.Cells[i, 14] = dtgrwDiemHocSinh.Rows[j].Cells[13].Value;
                ws.Cells[i, 15] = dtgrwDiemHocSinh.Rows[j].Cells[14].Value;
                ws.Cells[i, 16] = dtgrwDiemHocSinh.Rows[j].Cells[15].Value;
                ws.Cells[i, 17] = dtgrwDiemHocSinh.Rows[j].Cells[16].Value;
                ws.Cells[i, 18] = dtgrwDiemHocSinh.Rows[j].Cells[17].Value;
                i++;
            }
            ws.Cells[i+1, 17] = "Điểm trung bình:"+lblDiemTB.Text;
           

        }
    }
}
